﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CPPEI.PM
{
    /// <summary>
    /// 工作项类别
    /// </summary>
    [Serializable]
    public class WorkItemCategoryRef
    {
        /// <summary>
        /// 编号
        /// </summary>
        public Guid Id { get; set; }
        /// <summary>
        /// 描述
        /// </summary>
        public string Caption { get; set; }
        /// <summary>
        /// 类别编号
        /// </summary>
        public string Code { get; set; }

        /// <summary>
        /// 级别
        /// </summary>
        public int Level { get; set; }

        public WorkItemCategoryRef Parent { get; set; }

        /// <summary>
        /// 获取链表中获取特定的根节点
        /// </summary>
        /// <param name="depthFromRoot">从root开始计算，具体的depth值，这个值应该在[0,Level]</param>
        /// <returns></returns>
        public WorkItemCategoryRef GetFromRoot(int depthFromRoot)
        {
            int feedbackCount = Level - depthFromRoot;
            WorkItemCategoryRef parent = this;
            while (feedbackCount > 0)
            {
                parent = parent.Parent;
                feedbackCount--;
                if (parent == null)
                {
                    break;
                }
            }
            return parent;
        }

        /// <summary>
        /// 获取链表中code的路径
        /// </summary>
        /// <returns></returns>
        public string GetPathFromRoot()
        {
            return this.GetPathFromRoot(this.Level);
        }

        /// <summary>
        /// 获取链表中code的路径
        /// </summary>
        /// <param name="depthFromRoot">从root开始计算，具体的depth值，这个值应该在[0,Level]</param>
        /// <returns></returns>
        public string GetPathFromRoot(int depthFromRoot)
        {
            WorkItemCategoryRef parent = GetFromRoot(depthFromRoot);
            StringBuilder builder = new StringBuilder();
            while (depthFromRoot >= 0)
            {
                builder.Insert(0, string.Format("{0}\\", parent.Code));
                parent = parent.Parent;
                depthFromRoot--;
            }
            builder.Insert(0, "\\");
            return builder.ToString();
        }
    }
}
